﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using CivilleWP7.Views.Controls;
using System.Collections.ObjectModel;
using CivilleWP7.Civille.Source;
using System.Data.Services.Client;
using CivilleWP7.CivilleDataServiceReference;
using System.Windows.Media.Imaging;

namespace CivilleWP7
{
    public partial class UserProfilePage : PhoneApplicationPage
    {
        private bool IsLightTheme;
        public UserProfilePage()
        {
            InitializeComponent();
            setBackground();
            this.Loaded += new RoutedEventHandler(UserProfilePage_Loaded);
        }

        void UserProfilePage_Loaded(object sender, RoutedEventArgs e)
        {
            latestTasks.Items.Clear();
            CivilleDataServiceConnector.AllTasksQuery.BeginExecute(OnGetAllTasksCompleted, CivilleDataServiceConnector.AllTasksQuery);
        }
        void checkThemeColor()
        {
            var visibility = (Visibility)Application.Current.Resources["PhoneLightThemeVisibility"];
            if (visibility == Visibility.Visible)
                IsLightTheme = true;
            else
                IsLightTheme = false;
        }
        void setBackground()
        {
            checkThemeColor();
            ImageBrush myBrush = new ImageBrush();
            if(IsLightTheme)
                myBrush.ImageSource=new BitmapImage(new Uri("/Resources/Images/Backgroundwhite.jpg",UriKind.Relative));
            else
                myBrush.ImageSource = new BitmapImage(new Uri("/Resources/Images/Background.jpg",UriKind.Relative));
            LayoutRoot.Background=myBrush;
        }

        private void OnGetAllTasksCompleted(IAsyncResult result)
        {
            Dispatcher.BeginInvoke(() =>
            {
                List<Task> AllTasks = new List<Task>();
                DataServiceQuery<Task> query = result.AsyncState as DataServiceQuery<Task>;
                AllTasks = query.EndExecute(result).ToList();

                CivilleDataServiceConnector.SetTaskHistoryParameters(UserContext.CurrentUser.ID);
                CivilleDataServiceConnector.GetTaskHistory.BeginExecute((result2) =>
                {

                    Dispatcher.BeginInvoke(() =>
                    {
                        DataServiceQuery<TaskHistory> query2 = result2.AsyncState as DataServiceQuery<TaskHistory>;
                        foreach (TaskHistory history in query2.EndExecute(result2))
                        {
                            Task t = AllTasks.Where(x => x.ID == history.ID).First();
                            CustomListItem listItem = new CustomListItem();
                            listItem.TitleTxt.Text = t.Title;
                            listItem.DescriptionTxt.Text = t.Description;
                            listItem.CheckMarkImage.Source = new BitmapImage(new Uri(t.Logo, UriKind.RelativeOrAbsolute));
                            listItem.CheckMarkImage.Width = 50;
                            latestTasks.Items.Add(listItem);
                        }
                    });
                }, CivilleDataServiceConnector.GetTaskHistory);
            });
        }
    }
}
